package cn.lsc.org.controller;

import cn.lsc.basic.controller.util.JsonResult;
import cn.lsc.basic.controller.util.PageList;
import cn.lsc.org.domain.Employee;
import cn.lsc.org.query.EmployeeQuery;
import cn.lsc.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/emp")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService IEmployeeService;


    /*
    * 查询所有
    * */
    @GetMapping
    public List<Employee> findAll(){
       return IEmployeeService.findAll();
    }


    /*
    * 查询一个
    * */
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return IEmployeeService.findById(id);
    }

    /*
    * 修改和添加操作
    * */

    @PutMapping
    public JsonResult UpdateOrAdd(@RequestBody Employee Employee){
        try {
            if(Employee.getId()==null){
                IEmployeeService.add(Employee);
            }else {
                IEmployeeService.update(Employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    /*
    * 删除操作
    * */

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            IEmployeeService.del(id);
            return JsonResult.me();
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    /*
    * 分页查询
    * */

    @PostMapping
    public PageList<Employee> pageList(@RequestBody EmployeeQuery query){
        try {
           return IEmployeeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            IEmployeeService.patchDelete(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


}
